﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace BlackMofo.DAL.NHUtils
{
    public class NHHelper
    {
        #region 成员变量

        /// <summary>
        /// 会话工厂对象
        /// </summary>
        private ISessionFactory m_SessionFactory;
        /// <summary>
        /// NHibernate配置对象
        /// </summary>
        private Configuration m_Configuration;
        #endregion

        #region 成员属性
        /// <summary>
        /// 当前会话
        /// </summary>
        public ISession CurrentSession
        {
            get
            {
                return GetSession();
            }
        }

        /// <summary>
        /// 当前配置
        /// </summary>
        public Configuration Configuration
        {
            get
            {
                return m_Configuration;
            }
        }
        #endregion

        /// <summary>
        /// 无参构造
        /// </summary>
        public NHHelper()
        {
            m_Configuration = ConfigureNHibernate();
            m_SessionFactory = m_Configuration.BuildSessionFactory();
        }
        /// <summary>
        /// 获取会话工厂
        /// </summary>
        /// <returns></returns>
        public ISessionFactory GetSessionFactory()
        {
            return m_SessionFactory;
        }
        /// <summary>
        /// 获取会话
        /// </summary>
        /// <returns></returns>
        public ISession GetSession()
        {
            return m_SessionFactory.OpenSession();
        }

        /// <summary>
        /// 配置NHibernate
        /// </summary>
        /// <returns></returns>
        public static Configuration ConfigureNHibernate()
        {
            return new Configuration().Configure();
        }
    }
}
